//
//  LGFullScheduleHeader.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/11.
//

#import "LGFullScheduleHeader.h"
#import "FSCalendar.h"

@interface LGFullScheduleHeader()<FSCalendarDelegate,FSCalendarDataSource,FSCalendarDelegateAppearance>
/**   */
@property (nonatomic , strong)CAGradientLayer *shadowLayer;
/**   */
@property (nonatomic , strong)FSCalendar *calendar;
/**   */
@property (nonatomic , strong)UIButton *leftBtn;
/**   */
@property (nonatomic , strong)UIButton *rightBtn;
/**   */
@property (nonatomic , strong)UILabel *dateLabel;
/**  有日程 */
@property (nonatomic , strong)UIButton *scheduleBtn;
/**  有逾期 */
@property (nonatomic , strong)UIButton *overdueBtn;
/**   */
@property (nonatomic , strong)NSDateFormatter *monthFormatter;
/**   */
@property (nonatomic , strong)NSDateFormatter *dayFormatter;
/**   */
@property (nonatomic , assign)NSInteger currentYear;
/**   */
@property (nonatomic , assign)NSInteger currentMonth;
/**   */
@property (nonatomic , strong)NSMutableDictionary *eventDic;
@end

@implementation LGFullScheduleHeader

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
        [self.calendar selectDate:[NSDate date]];
    }
    return self;
}

-(void)setPointArray:(NSArray *)pointArray{
    _pointArray = pointArray;
    self.eventDic = [NSMutableDictionary dictionary];
    for (NSInteger i=0; i<pointArray.count; i++) {
        NSDictionary *tempDic = pointArray[i];
        [self.eventDic setValue:LGNSString(tempDic[@"status"]) forKey:LGNSString(tempDic[@"date"])];
    }
    [self.calendar reloadData];
}

//上个月 、下个月
-(void)monthBtnAction:(UIButton *)sender{
    NSDate *currentPatent = self.calendar.currentPage;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:(NSCalendarUnitMonth | NSCalendarUnitYear) fromDate:currentPatent];
    
    if (sender == self.leftBtn) {
        [components setMonth:components.month-1];
    }else{
        [components setMonth:components.month+1];
    }
    currentPatent = [calendar dateFromComponents:components];
    [self.calendar setCurrentPage:currentPatent animated:YES];
    self.dateLabel.text = [self.monthFormatter stringFromDate:currentPatent];
    
    NSInteger year = [[self.dateLabel.text substringToIndex:4] integerValue];
    NSInteger month = [[self.dateLabel.text substringWithRange:NSMakeRange(5, 2)] integerValue];
    
    if(self.delegate && [self.delegate respondsToSelector:@selector(monthRecordWithYear:month:)]){
        [self.delegate monthRecordWithYear:year month:month];
    }
    
//    if(year<self.currentYear || (year == self.currentYear && month<self.currentMonth)){
//        self.rightBtn.userInteractionEnabled = YES;
//        self.rightBtn.selected = NO;
//    }else{
//        self.rightBtn.userInteractionEnabled = NO;
//        self.rightBtn.selected = YES;
//    }
}
/*
 
 if([status isEqualToString:@"0"]){
     //没有点
     self.pointView.hidden = YES;
 }else if ([status isEqualToString:@"1"]){
     //有日程
     self.pointView.hidden = NO;
     self.pointView.backgroundColor = [UIColor colorWithString:@"#429BFF"];
 }else{
     //有逾期
     self.pointView.hidden = NO;
     self.pointView.backgroundColor = [UIColor colorWithString:@"#F84D4D"];
 }
 */

//eventPoint数量
-(NSInteger)calendar:(FSCalendar *)calendar numberOfEventsForDate:(NSDate *)date{
    NSString *dateStr = [self.dayFormatter stringFromDate:date];
    if ([self.eventDic.allKeys containsObject:dateStr]) {
        return 1;
    }
    return 0;
}

- (nullable NSArray<UIColor *> *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventDefaultColorsForDate:(NSDate *)date{
    NSString *dateStr = [self.dayFormatter stringFromDate:date];
    NSDictionary *colorDic = @{@"0":@"",@"1":@"#429BFF",@"2":@"#F84D4D"};
    NSString *colorStr = colorDic[self.eventDic[dateStr]];
    return colorStr.length>0?@[[UIColor colorWithString:colorStr]]:@[[UIColor clearColor]];
}


- (nullable NSArray<UIColor *> *)calendar:(FSCalendar *)calendar appearance:(FSCalendarAppearance *)appearance eventSelectionColorsForDate:(NSDate *)date{
    return @[[UIColor clearColor]];
}

//event图片
- (nullable NSArray *)calendar:(FSCalendar *)calendar imageArrayForDate:(NSDate *)date{
    NSString *dateStr = [self.dayFormatter stringFromDate:date];
    NSDictionary *pointDic = @{@"0":@"日历-事件",@"1":@"日历-有日程",@"2":@"日历-有逾期"};
    NSString *pointName = pointDic[self.eventDic[dateStr]];
    pointName = (pointName && pointName.length>0)?pointName:@"日历-事件";
    return @[pointName];
}

//点击选中日期
- (void)calendar:(FSCalendar *)calendar didSelectDate:(NSDate *)date atMonthPosition:(FSCalendarMonthPosition)monthPosition{
    if (date == calendar.today) {
        calendar.appearance.titleTodayColor = [UIColor colorWithString:@"#FFFFFF"];
    }else{
        calendar.appearance.titleTodayColor = [UIColor colorWithString:@"#2B2A37"];
    }
    NSString *dateStr = [self.dayFormatter stringFromDate:date];
    if (self.delegate && [self.delegate respondsToSelector:@selector(selectDate:)]) {
        [self.delegate selectDate:dateStr];
    }
}

//滑动、切换月份后默认选中日期
- (void)calendarCurrentPageDidChange:(FSCalendar *)calendar{
    self.dateLabel.text = [self.monthFormatter stringFromDate:calendar.currentPage];
    if (calendar.currentPage == calendar.today) {
        calendar.appearance.titleTodayColor = [UIColor colorWithString:@"#FFFFFF"];
    }else{
        calendar.appearance.titleTodayColor = [UIColor colorWithString:@"#2B2A37"];
    }
//    [calendar selectDate:calendar.currentPage];
    NSInteger year = [[self.dateLabel.text substringToIndex:4] integerValue];
    NSInteger month = [[self.dateLabel.text substringWithRange:NSMakeRange(5, 2)] integerValue];
    if(self.delegate && [self.delegate respondsToSelector:@selector(monthRecordWithYear:month:)]){
        [self.delegate monthRecordWithYear:year month:month];
    }
}


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.layer addSublayer:self.shadowLayer];
    [self addSubview:self.dateLabel];
    [self addSubview:self.leftBtn];
    [self addSubview:self.rightBtn];
    [self addSubview:self.calendar];
    [self addSubview:self.scheduleBtn];
    [self addSubview:self.overdueBtn];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(viewPix(8));
        make.centerX.equalTo(self);
    }];
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.dateLabel.mas_left).offset(-viewPix(23));
        make.width.height.equalTo(@(viewPix(30)));
        make.centerY.equalTo(self.dateLabel);
    }];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.dateLabel.mas_right).offset(viewPix(23));
        make.centerY.width.height.equalTo(self.leftBtn);
    }];
    [self.scheduleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.calendar.mas_bottom);
        make.right.equalTo(self.mas_centerX).offset(-viewPix(15));
        make.width.equalTo(@(viewPix(70)));
        make.height.equalTo(@(viewPix(22)));
    }];
    [self.overdueBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_centerX).offset(viewPix(15));
        make.centerY.width.height.equalTo(self.scheduleBtn);
    }];
}

-(CAGradientLayer *)shadowLayer{
    if(!_shadowLayer){
        _shadowLayer = [[CAGradientLayer alloc]init];
        _shadowLayer.frame = CGRectMake(0, 0, Screen_W ,viewPix(322));
        _shadowLayer.startPoint = CGPointMake(0.5, 0);
        _shadowLayer.endPoint = CGPointMake(0.5, 1.0);
        _shadowLayer.colors = @[(__bridge id)[UIColor colorWithString:@"#E5F3FF"].CGColor, (__bridge id)[UIColor colorWithString:@"#F3F9FE"].CGColor, (__bridge id)[UIColor colorWithString:@"#FEFEFF"].CGColor];
        _shadowLayer.locations = @[@(0), @(0.5), @(1.0f)];
    }
    return _shadowLayer;
}

-(UILabel *)dateLabel{
    if(!_dateLabel){
        _dateLabel = [UILabel lableWithText:[self.monthFormatter stringFromDate:[NSDate date]] colorString:@"#2B2A37" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
    }
    return _dateLabel;
}

-(UIButton *)leftBtn{
    if(!_leftBtn){
        _leftBtn = [UIButton buttonWithImage:@"日历-上个月" selectImage:@"日历-上个月"];
        [_leftBtn addTarget:self action:@selector(monthBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

-(UIButton *)rightBtn{
    if(!_rightBtn){
        _rightBtn = [UIButton buttonWithImage:@"日历-下个月" selectImage:@"日历-下个月"];
        [_rightBtn addTarget:self action:@selector(monthBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}

-(FSCalendar *)calendar{
    if(!_calendar){
        _calendar = [[FSCalendar alloc]initWithFrame:CGRectMake(viewPix(20), viewPix(40), Screen_W-viewPix(40), viewPix(250))];
        [_calendar setScope:FSCalendarScopeMonth animated:NO];
        _calendar.backgroundColor = [UIColor clearColor];
        _calendar.placeholderType =  FSCalendarPlaceholderTypeFillHeadTail;
        _calendar.weekdayHeight = viewPix(25);
        _calendar.rowHeight = viewPix(25);
        _calendar.headerHeight = 0;//设置顶部显示月份的高度
        _calendar.locale = [NSLocale localeWithLocaleIdentifier:@"zh-CN"];
        _calendar.appearance.todayColor = [UIColor clearColor];
        _calendar.appearance.caseOptions = FSCalendarCaseOptionsWeekdayUsesSingleUpperCase;
        _calendar.appearance.weekdayFont = LGFont(16);
        _calendar.appearance.weekdayTextColor = [UIColor colorWithString:@"#666666"];
        _calendar.appearance.titleFont = LGFont(18);
        _calendar.appearance.titleDefaultColor = [UIColor colorWithString:@"#444444"];
        _calendar.appearance.titlePlaceholderColor = [UIColor colorWithString:@"#A9A8B6"];
        _calendar.appearance.titleSelectionColor = [UIColor colorWithString:@"#3291FF"];
        _calendar.appearance.selectionColor = [UIColor colorWithString:@"#DFEEFF"];
//        _calendar.appearance.eventDefaultColor = [UIColor colorWithString:@"#009B77"];
//        _calendar.appearance.eventSelectionColor = [UIColor colorWithString:@"#FFFFFF"];
        _calendar.dataSource = self;
        _calendar.delegate = self;
    }
    return _calendar;
}

-(UIButton *)scheduleBtn{
    if(!_scheduleBtn){
        _scheduleBtn = [UIButton buttonWithTitle:@" 有日程" titleFont:LGFont(16) textColor:@"#666666" imageName:@"日程-有日程"];
        _scheduleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        _scheduleBtn.userInteractionEnabled = NO;
    }
    return _scheduleBtn;
}

-(UIButton *)overdueBtn{
    if(!_overdueBtn){
        _overdueBtn = [UIButton buttonWithTitle:@" 有逾期" titleFont:LGFont(16) textColor:@"#666666" imageName:@"日程-有逾期"];
        _overdueBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _overdueBtn.userInteractionEnabled = NO;
    }
    return _overdueBtn;
}

-(NSDateFormatter *)monthFormatter{
    if(!_monthFormatter){
        _monthFormatter = [[NSDateFormatter alloc]init];
        _monthFormatter.dateFormat = @"yyyy年MM月";
    }
    return _monthFormatter;
}

-(NSDateFormatter *)dayFormatter{
    if(!_dayFormatter){
        _dayFormatter = [[NSDateFormatter alloc]init];
        _dayFormatter.dateFormat = @"yyyy-MM-dd";
    }
    return _dayFormatter;
}
@end
